
package xj.graph2d.util;

import java.util.*;

import xj.graph2d.DrawShape;

public class DrawShapeList { 

  public final int count() {
    return shapes.size();
  }

  public void add(DrawShape s) {
    shapes.add(s);
  }

  public void add(int i, DrawShape s) {
    if (s != null) {
      int n = shapes.size();
      if (i < 0) {
	i = 0;
      }
      if (i > n) {
	i = n;
      }
      shapes.add(i, s);
    }
  }

  public int find(DrawShape s) {
    if (s != null) {
      int n = shapes.size();
      for (int i = 0; i < n; i++) {
	if (s == shapes.get(i)) {
	  return i;
	}
      }
    }
    return -1;
  }

  public DrawShape findInRole(int role) {
    if (shapes != null) {
      int n = shapes.size();
      for (int i = 0; i < n; i++) {
	DrawShape s = shapes.get(i);
	if (s.getRole() == role) {
	  return s;
	}
      }
    }
    return null;
  }

  public void remove(DrawShape s) {
    if (s != null) {
      int n = shapes.size();
      for (int i = 0; i < n; i++) {
	if (s == shapes.get(i)) {
	  shapes.remove(i);
	  break;
	}
      }
    }
  }

  public void remove(int i) {
    shapes.remove(i);
  }

  public void removeAll() {
    shapes.clear();
  }

  public DrawShape get(int i) {
    if (i >= 0 && i < shapes.size()) {
      DrawShape shape = (DrawShape) shapes.get(i);
      return shape;
    } else {
      return null;
    }
  }

  public List<DrawShape> getShapes() {
    return shapes;
  }

  public Iterator<DrawShape> iterator() {
    return shapes.iterator();
  }

  public void move(float dx, float dy) { 
    for (DrawShape s : shapes) { 
      s.move(dx, dy);
    }
  }

  protected List<DrawShape> shapes = new ArrayList<DrawShape>(); 

}